﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DeepLearning;


public class ModelPlaneLoader: MonoBehaviour
{
    public static Dictionary<string, List<Vector3>> planes = new Dictionary<string, List<Vector3>>();

    private void Start()
    {
        using (StreamReader sr = new StreamReader(Params.modelPlane_pth))
        {
            Debug.Log(Params.modelPlane_pth);
            string line;
            int modelCount = 0;
            while (true)
            {
                line = sr.ReadLine();
                if (line.Contains("modelCount"))
                    break;
            }

            if (line.Split(' ').Length >= 2)
                modelCount = Int32.Parse(line.Split(' ')[1]);

            for (int i = 0; i < modelCount; i++)
            {
                line = sr.ReadLine();
                loadLine(line);
            }
        }
    }

    private void loadLine(string line)
    {
        string[] values = line.Split(',');
        for (int i = 0; i < values.Length; i++)
            values[i] = values[i].Trim();

        if (values.Length != 9)
            Debug.Log("ModelPlanes.txt format error");

        string key = values[0];
        List<Vector3> list = new List<Vector3>();
        for (int i = 1; i < values.Length; i++)
        {
            string[] vectors = values[i].Split(' ');
            list.Add(new Vector3(float.Parse(vectors[0]), float.Parse(vectors[1]), float.Parse(vectors[2])));
        }

        if (list.Count != 8)
            Debug.Log("read plane points error");

        if (planes.ContainsKey(key))
            Debug.Log("model repeated");
        else
            planes.Add(key, list);
    }
}